import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({
  /**
  * 页面的初始数据
  */
  data: {
    tabs: [ 
      {
        id: 0,
        value: "综合",
        isActive: true
      },
      {
        id: 0,
        value: "综合",
        isActive: false
      },
      {
        id: 0,
        value: "综合",
        isActive: false
      }
    ],
    goodsList:[]
  },
  //总页数
  totalPages:1,
  //接口要的参数
  QueryParams:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:10
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.QueryParams.cid = options.cid;
    this.getGoodsList();
  },
 
  //获取商品列表数据
  async getGoodsList(){
    const res = await request({url:"/goods/search",data:this.QueryParams});
    //获取总条数
    const total = res.total;
    //计算总页数
    this.totalPages = Math.ceil(total/this.QueryParams.pagesize);
    console.log(this.totalPages);
    this.setData({
      goodsList : [...this.data.goodsList,...res.goods]
    })
  },

  //标题点击事件 从子组件传递过来的
  handleTabsItemChange(e){
    //获取被点击的标题索引
      const {index} = e.detail;
    //修改数据源
    let {tabs} = this.data;
    tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
    //赋值到data中
    this.setData({
      tabs
    })
  },
  onReachBottom(){
    if(this.QueryParams.pagenum>= this.totalPages){//没有下一页
      wx.showToast({
        title: '没有下一页了'
      });
        
    }else{
      this.QueryParams.pagenum++;
      this.getGoodsList();
    }
  }
})